﻿/**
 *		消息和消息函数的定义
 */
#ifndef MSGDEFINE_H
#define MSGDEFINE_H
#include <ossType.h>
#include <ossList.h>
#include <string.h>
#include <ossObject.h>

//
/**
 *		命令消息
 *@消息函数形式为	void fun(char* cmd,int cmdLen,int dataLen);
 *							cmd 命令的开始
 *							cmdLen	命令内容长度
 *							dataLen 附加数据长度,附加数据为 cmd + cmdLen
 */
#define MSG_TYPE_COMMAND            1

/**
 *		自定义消息,消息为大于MSG_USER的消息
 *@消息函数形式为	void fun(WParam wparam,LParam lparam);
 *							message 消息标志
 *							wparam	参数
 *							lparam	参数
 */
#define MSG_TYPE_MESSAGE			2


/**
 *		广播消息
 */
#define MSG_TYPE_BOARDCAST_MESSAGE			3

#define MSG_TYPE_BOARDCAST_COMMAND			4


/**
 *		系统消息定义
 */
//销毁消息
#define MSG_SDESTROY				1
//心跳检查消息
#define	MSG_HEARTBEAT				2

//系统消息与用户消息的分界点
#define MSG_USER					1024


//消息返回值定义
#define MSG_OK					0	//命令或消息被成功执行
#define MSG_ASYNCHRONOUS_OK		1		//异步OKasynchronous
#define MSG_HANDING_ELSEWHERE	2		//交给其他地方处理Handling elsewhere
#define MSG_NOFUNCTION			(-1)	//没有找到映射函数
#define MSG_SESSION_ERROR		(-2)
#define MSG_EXECUTE_ERROR		(-3)	//执行错误
#define MSG_INVALID_FUNCTION	(-4)	//无效的函数格式
#define MSG_PARAM_ERROR			(-5)	//参数错误
#define MSG_NOOPPOSITE			(-6)	//对象不存在
#define MSG_BREAK_BROADCAST		(-7)	//停止广播
#define MSG_MEMORY_ERROR		(-8)	//内存错误，如内存不足
#define MSG_OBJ_INVAILABLE		(-9)	//对象已不可用
#define MSG_NOT_SUPPORT			(-10)	//不支持
#define MSG_NOT_FOUND			(-11)	//没有发现


//名称长度
#define USERNAME_LEN_MAX		20

//ATL/MFC code should use standard pointer to member standard syntax &MyClass::MyMethod, instead
//of the legacy non-standard syntax - MyMethod.
#ifdef WINDOWS_OS
#define PTM_WARNING_DISABLE \
	__pragma(warning( push )) \
	__pragma(warning( disable : 4867 ))\
	__pragma(warning(disable : 4311))\
	__pragma(warning(disable : 4407))
#define PTM_WARNING_RESTORE \
	__pragma(warning( pop ))\
	__pragma(warning(pop))\
	__pragma(warning(pop))
#else
#define PTM_WARNING_DISABLE
#define PTM_WARNING_RESTORE

#endif //WINDOWS_OS

#ifndef MFH_MSG_CALL
#define MFH_MSG_CALL
#endif
/**
 *		函数类型定义
 */
typedef enum{
	OSS_iwl,	//WParam和Lparam
	OSS_iv,		//返回值为int 没有参数
    OSS_is,     //返回值为int,参数为char*
	OSS_ijj,	//返回值为int,参数为json,json

	OSS_end
}CALLBACKTYPE;


/**
 *		处理回调函数
 *@return	
 */
typedef int (MFH_MSG_CALL OssObject::*OSS_PMSG)(void);

/**
 *		消息结构体定义
 */
typedef struct MsgEntry_tag
{
	DWord		nMessageType;   // 消息类型
	DWord		nFirstCode;			// 消息ID
	DWord		nLastCode;		//最后一个ID
	const char*		cmd;			//命令
	DWord		nSig;			// signature type (action) or pointer to message #
	OSS_PMSG	    pfn;			// routine to call (or special value)
}MsgEntry;

/**
 *		消息类型定义
 */
typedef struct MsgItem_tag
{
	OssListNode	node;
	void*	from;
	void*	to;
	DWord		nMessageType;   // 消息类型

	union
	{
		DWord		nCode;			// 消息ID
		const char* cmd;			//命令
	};
	
	WParam		wparam;			//命令参数 1
	LParam		lparam;			//命令参数 2
}MsgItem,*LPMsgItem;



/**
 *		获取消息映射表的定义
 */
struct OSS_MSGMAP
{
	const char* className;
	const long	classOffset;
	const OSS_MSGMAP* (* pfnGetBaseMap)();
	const MsgEntry* lpEntries;
};

//类中声明该宏后，即可实现消息映射表
#define DECLARE_OSS_MESSAGE_MAP() \
protected: \
	static const OSS_MSGMAP* OssGetThisMessageMap(); \
	virtual const OSS_MSGMAP* OssGetMessageMap() const; \


#define BEGIN_OSS_MESSAGE_MAP(theClass, baseClass) \
	PTM_WARNING_DISABLE \
	const OSS_MSGMAP* theClass::OssGetMessageMap() const \
{ return theClass::OssGetThisMessageMap(); } \
	const OSS_MSGMAP* theClass::OssGetThisMessageMap() \
{ \
	typedef theClass ThisClass;						   \
	typedef baseClass TheBaseClass;					   \
	static const char* className = ""#theClass;\
	static const long classOffset = (long)((theClass*)((baseClass*)1)) - 1;\
	static const MsgEntry _messageEntries[] =  \
{

#define END_OSS_MESSAGE_MAP() \
{0,  0, 0,NULL, (DWord)OSS_end, (OSS_PMSG)0 } \
}; \
	static const OSS_MSGMAP messageMap = \
{ className,classOffset,&TheBaseClass::OssGetThisMessageMap, &_messageEntries[0] }; \
	return &messageMap; \
}								  \
	PTM_WARNING_RESTORE

/**
 *		定义命令消息
 */
#define OSS_COMMAND(cmd,memberFun) { MSG_TYPE_COMMAND, 0, 0,cmd, OSS_iwl, \
	(OSS_PMSG) (static_cast <int (OssObject::*)(WParam,LParam)>(&memberFun)) },

/**
 *		定义用户自定义消息
 */
#define OSS_MESSAGE(msg,memberFun) {MSG_TYPE_MESSAGE,msg,msg,NULL,OSS_iwl,\
	(OSS_PMSG)(static_cast< int (OssObject::*)(WParam,LParam) > (&memberFun))},

 /**
  *		定义用户自定义请求消息
  */
#define OSS_REQUEST_MESSAGE(msg,memberFun) {MSG_TYPE_MESSAGE,msg,msg,NULL,OSS_ijj,\
	(OSS_PMSG)(static_cast< int (OssObject::*)(const OssJson&request,OssJson&response) > (&memberFun))},

  /**
   *		定义命令请求命令
   */
#define OSS_REQUEST_COMMAND(cmd,memberFun) { MSG_TYPE_COMMAND, 0, 0,cmd, OSS_ijj, \
	(OSS_PMSG) (static_cast <int (OssObject::*)(const OssJson&request,OssJson&response)>(&memberFun)) },
#endif
